
function EventsEmit() {
    this._events = {}
};

EventsEmit.prototype.on = function (key, callback) {
    if (!this._events) this._events = {}
    let callbacks = this._events[key] || []
    callbacks.push(callback)
    this._events[key] = callbacks
}

EventsEmit.prototype.emit = function (key, ...args) {
    this._events[key].forEach(cb => cb(...args))
}


EventsEmit.prototype.off = function (key, callback) {
    if (!this._events) this._events = {}
    let callbacks = this._events[key] || []
    this._events[key] = callbacks.filter(cb => cb !== callback && cb.l !== callback)
}


EventsEmit.prototype.once = function (key, callback) {
    const one = (...args) => {
        callback(...args)
        this.off(key, one)
    }
    one.l = callback // once被off，由于cb被one切片 用l记录cb，用于off 对应的one
    this.on(key, one)
}


module.exports = EventsEmit;